package com.want.repository;

import com.want.entity.Blog;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDate;

/**
 * @author WangZhiJian
 * @since 2021/11/28 0028
 */
@Repository
public interface BlogRepository extends ReactiveCrudRepository<Blog,Integer> {

    @Query("update blog set read_count = read_count + :count where id = :id")
    Mono<Integer> increaseRead(Integer id,Integer count);

    @Modifying
    @Query("update blog set good_count = good_count + :count where id = :id")
    Mono<Integer> incrGoodCount(Integer id,Integer count);

    @Query("SELECT DATE_FORMAT(create_time, '%Y-%m-%d') name, COUNT(*) count FROM blog WHERE online = 1 and create_time >= :start and create_time <= :end GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')")
    <T> Flux<T> getBlogContributeCountAndDates(LocalDate start,LocalDate end,Class<T> type);

    @Query("SELECT type_id name, COUNT(*) count FROM blog WHERE online = 1 GROUP BY type_id")
    <T> Flux<T> getBlogCountByBlogType(Class<T> type);

    @Query("SELECT tag_ids name, COUNT(*) count FROM blog WHERE online = 1 GROUP BY tag_ids")
    <T> Flux<T> getBlogCountByBlogTag(Class<T> type);

}
